
<?php

/** 
 * 
 */
class ReturnServiceCarOrder extends Eloquent
{
    protected $table = "return_service_card_order";
    
    public static $reason = array(
        "0"=>"无理由退货",
    );
    public static $accredit_type = array(
        "0"=>"账号授权",
        "1"=>"动态授权",
    );   


    /**
     * @Author:      lyt
     * @DateTime:  2017-03-27 16:17:03
     * @Description: 订单的退款
     */
    public static function refund($return_order_id,$approver,$approver_name,$return_type){
	    return DB::transaction(function () use ($return_order_id,$approver,$approver_name,$return_type) {
	        $ro=ReturnServiceCarOrder::find($return_order_id);//查找套餐卡退款单
	        $order=ServiceCardOrder::find($ro->order_id);
	        $user = Users::find($order->user_id);
	        $mc=MemberCoefficient::where("source",3)->where("status",1)->first();
          $ms=MaintenanceShops::find($order->shop_id);
          $s=0;
          foreach($return_type as $rt){
              $s+=$rt["amount"]*100;
              $tran_no = date('YmdHis',time()).Common::generateCode(8);
              $opr=ServiceCardOrderPayRecord::find($rt["id"]);
              if(is_null($opr) || $opr->status==0){
                  $response = Lang::get("scs_error.order_return_error_40050");
                  break;
              }
              if($rt["amount"]*100 > $opr->amount){
                  $response = Lang::get("scs_error.order_return_error_40050");
                  break;
              }
              $ror=new ReturnServiceCardRecord;
              $ror->order_pay_id=$rt["id"];
              $ror->return_order_id=$return_order_id;
              $ror->pay_type=$opr->pay_type;
              $ror->amount=$rt["amount"]*100;
              $ror->save();
              $order_amount=$ror->amount;
              if($ror->pay_type== 1 ||$ror->pay_type== 2 ||$ror->pay_type== 3 || $ror->pay_type== 7||$ror->pay_type== 3){//现金 或者银行卡支付 或者微信支付
                    Account::record($order->user_id,$order->shop_id,1,$tran_no,1,127,$order_amount,'套餐卡订单退单收入,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                    //记录用户收入
                    Account::record($order->user_id,$order->shop_id,1,$tran_no,2,227,$order_amount,'套餐卡订单退单支出,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                    //记录公司账户支出
                    Account::record_base('1',$tran_no,2,227,$order_amount,'套餐卡订单退单支出,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                    //记录订单账务关系
                    ServiceCardOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
              }else if($ror->pay_type == 8){//个人余额 暂时不判断 因为个人 余额现在都为空
                    $arr=AcountUserRechargeSpend::getUseRecharge($opr->id,$order_amount,8);
                      if(sizeof($arr)>0){
                        foreach ($arr as $_arr) {
                          //记录了用户消费退款记录
                          $aurs=new AcountUserRechargeSpendRe;
                          $aurs->aurr_id=$_arr->aurr_id;
                          $aurs->user_id=$_arr->user_id;
                          $aurs->aurr_sp_id=$_arr->id;                      
                          $aurs->buss_id=$ror->id;
                          $aurs->type=7;//套餐卡订单退款
                          $aurs->amount=$_arr->user_amount;
                          $aurs->status=1;
                          $aurs->tran_no=$tran_no;
                          $aurs->shop_id=$order->shop_id;
                          $aurs->implement_time=date("Y-m-d H:i:s",time());
                          $aurs->save();
                          //更改充值记录
                          DB::update('update acount_user_recharge_record set current_amount = current_amount+? where id = ?', array($_arr->user_amount,$_arr->aurr_id));
                          //更改充值记录消费记录
                          AcountUserRechargeSpend::where('id',$_arr->id)->update(array('return_amount'=> $_arr->return_amount +$_arr->user_amount,));
                          //记录个人 各种账户余额
                          $sac=AcountUserRecharge::find($_arr->aurr_id);
                          Account::record($order->user_id,$sac->shop_id,$_arr->acount_type,$tran_no,1,127,$_arr->user_amount,'套餐卡订单退单收入,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                          //记录公司账户支出
                          $base_acount_num=$_arr->acount_type==1?1:3;//如果用的现金账户 计入企业现金帐 如果是虚拟充值记录
                          Account::record_base($base_acount_num,$tran_no,2,227,$_arr->user_amount,'套餐卡订单退单支出,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                          
                        }
                        ServiceCardOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
                      }else{
                        DB::rollback();
                        $response = Lang::get("scs_error.order_user_notpay_40039");
                        return $response;
                      } 
              }else if($ror->pay_type== 6){//企业挂账
                  Account::record($order->user_id,0,3,$tran_no,1,127,$order_amount,'套餐卡订单退单收入,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                  ServiceCardOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));
              }else{
                Account::record($order->user_id,$order->shop_id,1,$tran_no,1,127,$order_amount,'套餐卡订单退单收入,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                //记录用户收入
                Account::record($order->user_id,$order->shop_id,1,$tran_no,2,227,$order_amount,'套餐卡订单退单支出,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                //记录公司账户支出
                Account::record_base('1',$tran_no,2,227,$order_amount,'套餐卡订单退单支出,套餐卡退货单号:'.$ro->id.',套餐卡订单单号:'.$ro->order_id);
                //记录订单账务关系
                ServiceCardOrderAccount::insert(array('order_id' => $ro->id,'tran_no' => $tran_no,'type' => 2,));                
              }

              if(in_array($ror->pay_type,ServiceCardOrderPayRecord::$ei_pay_type)){//说明是返回积分的支付类型                

                UserIntegralFlow::record($order->user_id,$order->shop_id,$ro->id,2,2003,floor($order_amount*$mc->integral*$ms->integral/100),'套餐卡订单退款扣除积分');
                UserExperienceFlow::record($order->user_id,$order->shop_id,$ro->id,2,2003,floor($order_amount*$mc->experience*$ms->experience/100),'套餐卡订单退款扣除经验');                
              }
          }
          Users::checkMember($order->user_id);
	        //消除 套餐卡的状态
          ServiceCardDetail::where('id',$order->service_card_detail_id)->update(array("status"=>0));
	         
          $ro->amount=$s;
	        $ro->approver=$approver;
	        $ro->approver_name=$approver_name;
	        $ro->status=1;
	        $ro->implement_time=date("Y-m-d H:i:s",time()+Config::get("config.report_time_diff"));
	        $ro->save();
	        return array('code' => 0, 'msg' => 'ok', 'result' => array("return_order_id"=>$return_order_id));
	    });
    }
}